//
//  BAHomePictureViewCell.swift
//  BAweibo
//
//  Created by baggio on 2017/8/20.
//  Copyright © 2017年 baggio. All rights reserved.
//

import UIKit
import SDWebImage

class BAHomePictureViewCell: UICollectionViewCell {
    
    //提供一个属性供外界赋值
    //必须用可选值，只有可选值才可以在构造函数中不初始化
    var picUrl: BAPictureUrlModel? {
        didSet{
            bgImageView.ba_sd_setImage(urlString: picUrl?.thumbnail_pic, placeholderImageName: "timeline_image_placeholder")
            
            //gif
//            print(picUrl?.thumbnail_pic)
//            http://wx4.sinaimg.cn/thumbnail/9fe432b5gy1fi7sux2qaqj21120kugoo.gif"
            if let thumbnail = picUrl?.thumbnail_pic, thumbnail.hasSuffix(".gif") {
                gifImageView.isHidden = false
            } else {
                gifImageView.isHidden = true
            }
        }
    }
 
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setUpUI(){
        backgroundColor = randomColor()
        
        addSubview(bgImageView)
        addSubview(gifImageView)
        
        bgImageView.snp.makeConstraints { (m) in
            m.left.top.right.bottom.equalTo(self)
            //也可以
//            m.edges.equalTo(self)
        }
        
        gifImageView.snp.makeConstraints { (m) in
            m.right.bottom.equalTo(self)
        }
        
    }
    
//    private lazy var bgImageView: UIImageView = UIImageView(imgName: "timeline_image_placeholder")
    
    private lazy var bgImageView: UIImageView = {
        let image = UIImageView(imgName: "timeline_image_placeholder")
        
        //图片填充方式默认是拉伸撑满（scaleToFill），不好看，改成短边完全显示，长边裁剪（scaleAspectFill）；另外还有一种，长边完全显示（scaleAspectFit）
        image.contentMode = .scaleAspectFill
        //多出来的要裁减掉
        image.clipsToBounds = true
        
        return image
    }()
    
    //gif标记
    private lazy var gifImageView: UIImageView = UIImageView(imgName: "timeline_image_gif")
}
